package com.jiuwu.spring.eshop.controller;

import com.jiuwu.spring.eshop.db.entity.Comment;
import com.jiuwu.spring.eshop.respository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class CommentController {

    @Autowired
    CommentRepository commentRepository;



    @RequestMapping("/manage/comment_manage")
    public String  getComments(Model model){
        List<Comment> comments = commentRepository.findAll();
        model.addAttribute("comments",comments);

        return "/manage/comment_manage";

    }

    @RequestMapping("/manage/comment_reply/{orderId}")
    public String  getComment(@PathVariable("orderId")String orderId, Model model) {

        Integer id = Integer.valueOf(orderId);
        List<Comment> comments = commentRepository.findOrder(id);
        if(comments.size()>0) {
            model.addAttribute("comment", comments.get(0));
        }
        return "/manage/comment_reply";
    }

    @RequestMapping("/manage/comment_reply_deal/{orderId}")
    public String  dealComment(@PathVariable("orderId")String orderId,
                               @RequestParam("commentReply")String commentReply){

        Integer id = Integer.valueOf(orderId);
        commentRepository.updateMemberInfo(id,commentReply);

        return "redirect:/manage/index?id=18";

    }

    @RequestMapping("/manage/comment_del/{orderId}")
    public String delComment(@PathVariable("orderId")Integer id){

        commentRepository.delOrder(id);
        return "redirect:/manage/index?id=18";
    }

//    @RequestMapping("/comment_add/{orderId}" )
//    public String addComment(@PathVariable("orderId") Integer orderId,
//                             Model model){
//        model.addAttribute("orderId",orderId);
//        return "/comments";
//    }
//
//
//    @RequestMapping(value = "/comment_deal/{orderId}",method = RequestMethod.POST)
//    public String handleAddComment(@PathVariable("orderId")Integer orderId,
//                                   @RequestParam("content") String content,
//                                   HttpSession session,
//                                   RedirectAttributes redirectAttributes){
//
//        String userName = (String) session.getAttribute("userName");
//        if(StringUtils.isEmpty(userName)){
//            redirectAttributes.addFlashAttribute("hasUser",false);
//            return "redirect:/comment_add/"+orderId;
//        }else{
//
//        }
//        return "";
//    }





    //------------------------------------thymeleaf-------------------------------------
    @RequestMapping(value = {"/manage/comment","/manage/comment/{pageNum}"})
    public String getComments2(@PathVariable(value = "pageNum",required = false)Integer pageNum,Model model) {
        pageNum = pageNum == null ? 0 : pageNum > 0 ? pageNum - 1 : 0;
        Page<Comment> commentPage = commentRepository.findAll(PageRequest.of(pageNum, 5));
        model.addAttribute("commentPage", commentPage);

        return "comment";
    }

    @RequestMapping("/manage/delComment/{commentId}")
    public String delComment2(@PathVariable("commentId")Integer id){
        commentRepository.delOrder(id);
        return "redirect:/manage/comment";
    }
}
